//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ClosestRounding.h"
using namespace Cephei::QL::Math;
#include <gen/QL/Math/Rounding.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CClosestRounding::CClosestRounding (Int32 precision, Microsoft::FSharp::Core::FSharpOption<Int32>^ digit) : CRounding(CClosestRounding::typeid)
{
    try
    {
#ifdef HANDLE
        _phClosestRounding = NULL;
#endif
        QuantLib::Integer _precision = (QuantLib::Integer)ValueHelper::Convert (precision); //d
        QuantLib::Integer _digit = 
            (Microsoft::FSharp::Core::FSharpOption<Int32>::IsSome::get (digit) ? (QuantLib::Integer)ValueHelper::Convert (digit->Value) : 5); //4
        _ppClosestRounding = new boost::shared_ptr<QuantLib::ClosestRounding> (new QuantLib::ClosestRounding ( _precision,  _digit ));
        SetRounding (boost::dynamic_pointer_cast<QuantLib::Rounding> (*_ppClosestRounding));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::CClosestRounding::CClosestRounding (boost::shared_ptr<QuantLib::ClosestRounding>& childNative, Object^ owner) : CRounding(CClosestRounding::typeid)
{
#ifdef HANDLE
	_phClosestRounding = NULL;
#endif
	_ppClosestRounding = &childNative;
    _ppRounding = new boost::shared_ptr<QuantLib::Rounding> (boost::dynamic_pointer_cast<QuantLib::Rounding> (*_ppClosestRounding));
}
Cephei::QL::Math::CClosestRounding::CClosestRounding (QuantLib::ClosestRounding& childNative, Object^ owner) : CRounding(CClosestRounding::typeid)
{
#ifdef HANDLE
	_phClosestRounding = NULL;
#endif
	_ppClosestRounding = new boost::shared_ptr<QuantLib::ClosestRounding> (&childNative);
    _ppRounding = new boost::shared_ptr<QuantLib::Rounding> (boost::dynamic_pointer_cast<QuantLib::Rounding> (*_ppClosestRounding));
    _ClosestRoundingOwner = owner;
    _RoundingOwner = owner;
}

Cephei::QL::Math::CClosestRounding::CClosestRounding (CClosestRounding^ copy) : CRounding(CClosestRounding::typeid)
{
#ifdef HANDLE
	_phClosestRounding = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppClosestRounding = new boost::shared_ptr<QuantLib::ClosestRounding> (copy->GetShared());
        _ppRounding = new boost::shared_ptr<QuantLib::Rounding> (boost::dynamic_pointer_cast<QuantLib::Rounding> (*_ppClosestRounding));
    }
}
Cephei::QL::Math::CClosestRounding::CClosestRounding (PLATFORM::Type^ t) : CRounding(CClosestRounding::typeid)
{
#ifdef HANDLE
	_phClosestRounding = NULL;
#endif
	if (!t->IsSubclassOf(CClosestRounding::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CClosestRounding::CClosestRounding (QuantLib::Handle<QuantLib::ClosestRounding>& childNative, Object^ owner)  : CRounding(CClosestRounding::typeid)
{
	_phClosestRounding = &childNative;
	_ppClosestRounding = &static_cast<boost::shared_ptr<QuantLib::ClosestRounding>>(childNative.currentLink());
    _ppRounding = new boost::shared_ptr<QuantLib::Rounding> (boost::dynamic_pointer_cast<QuantLib::Rounding> (*_ppClosestRounding));
    _ClosestRoundingOwner = owner;
}
Cephei::QL::Math::CClosestRounding::CClosestRounding (QuantLib::Handle<QuantLib::ClosestRounding> childNative)  : CRounding(CClosestRounding::typeid)
{
	_phClosestRounding = &childNative;
	_ppClosestRounding = &static_cast<boost::shared_ptr<QuantLib::ClosestRounding>>(childNative.currentLink());
    _ppRounding = new boost::shared_ptr<QuantLib::Rounding> (boost::dynamic_pointer_cast<QuantLib::Rounding> (*_ppClosestRounding));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CClosestRounding::CClosestRounding (QuantLib::ClosestRounding childNative)  : CRounding(CClosestRounding::typeid)
{
#ifdef HANDLE
	_phClosestRounding = NULL;
#endif
	_ppClosestRounding = new boost::shared_ptr<QuantLib::ClosestRounding> (new QuantLib::ClosestRounding (childNative));
    _ppRounding = new boost::shared_ptr<QuantLib::Rounding> (boost::dynamic_pointer_cast<QuantLib::Rounding> (*_ppClosestRounding));
}
#endif

Cephei::QL::Math::CClosestRounding::~CClosestRounding ()
{
    if (_ppClosestRounding != NULL)
    {
	    delete _ppClosestRounding;
        _ppClosestRounding = NULL;
    }
}
Cephei::QL::Math::CClosestRounding::!CClosestRounding ()
{
    if (_ppClosestRounding != NULL)
    {
	    delete _ppClosestRounding;
    }
}
QuantLib::ClosestRounding& Cephei::QL::Math::CClosestRounding::GetReference ()
{
    if (_ppClosestRounding == NULL) throw REFNEW NativeNullException ();
	return **_ppClosestRounding;
}
boost::shared_ptr<QuantLib::ClosestRounding>& Cephei::QL::Math::CClosestRounding::GetShared ()
{
    if (_ppClosestRounding == NULL) throw REFNEW NativeNullException ();
	return *_ppClosestRounding;
}
QuantLib::ClosestRounding* Cephei::QL::Math::CClosestRounding::GetPointer ()
{
    if (_ppClosestRounding == NULL) throw REFNEW NativeNullException ();
	return &**_ppClosestRounding;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ClosestRounding>& Cephei::QL::Math::CClosestRounding::GetHandle ()
{
	if (_phClosestRounding == NULL)
	{
		_phClosestRounding = new Handle<QuantLib::ClosestRounding> (*_ppClosestRounding);
	}
	return *_phClosestRounding;
}
#endif
bool Cephei::QL::Math::CClosestRounding::HasNative () 
{
	return (_ppClosestRounding != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::IClosestRounding^ Cephei::QL::Math::CClosestRounding_Factory::Create (Int32 precision, Microsoft::FSharp::Core::FSharpOption<Int32>^ digit)
{
    return REFNEW CClosestRounding ( precision,  digit);
}
